package com.ys168.gam.shortestpath.view;

import static com.ys168.gam.shortestpath.uimessage.UIMessageUtil.getString;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

import com.ys168.gam.shortestpath.uimessage.UIMessageCode;
import com.ys168.gam.shortestpath.uimessage.UIMessageUtil;

/**
 *
 * @author Jason Krebs
 * @date 2015年2月10日
 */
public class InformationDialog extends JDialog {

    private static final long serialVersionUID = -1054799924855356105L;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        InformationDialog dialog = new InformationDialog();
        dialog.setVisible(true);
    }

    /**
     * Create the dialog.
     */
    public InformationDialog() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(
                InformationDialog.class.getResource("/image/CrystalCircle.png")));
        setModal(true);
        setBounds(100, 100, 600, 500);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - getWidth()) / 2, (screen.height - getHeight()) / 2);
        setTitle(getString(UIMessageCode.INFORMATION));
        createContent();
    }

    private void createContent() {
        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        try {
            JEditorPane editorPane = new JEditorPane(UIMessageUtil.getInformationUrl());
            editorPane.setEditable(false);
            editorPane.setContentType("text/html;charset=UTF-8");
            scrollPane.setViewportView(editorPane);
        } catch (IOException e1) {
            System.err.println(e1.getMessage());
        }

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);

        JButton closeButton = new JButton(getString(UIMessageCode.CLOSE));
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InformationDialog.this.dispose();
            }
        });
        buttonPane.add(closeButton);
        getRootPane().setDefaultButton(closeButton);

        JViewport viewPort = scrollPane.getViewport();
        viewPort.setViewPosition(new Point(0, 0));
    }

}
